<div style="font-size: 16px; color: #000; padding: 10px 0px 5px 5px; font-weight: bold;">当期开奖时间</div>
<div class="form_content current">
    <table class="form-table-without-line">
        <tr>
            <th>期号</th>
            <td>
                <input type="text" class="am-form-field qihao" placeholder="请输入当前开奖的期号">
            </td>
        </tr>
        <tr>
            <th>封盘时间</th>
            <td>
                <input type="text" value="" class="am-form-field closeTime">
            </td>
        </tr>
        <tr>
            <th>开奖时间</th>
            <td>
                <input type="text" value="" class="am-form-field openTime">
                <button class="save_curr">保存</button>
            </td>
        </tr>
    </table>
</div>

<div style="font-size: 16px; color: #000; padding: 10px 0px 5px 5px; font-weight: bold;">开奖结果设置</div>
<div class="form_content">
    <table class="form-table-without-line result2">
        <tr>
            <th>期号</th>
            <td>
                <input type="text" class="am-form-field issue" placeholder="输入待设置的期数">
            </td>
        </tr>
        <tr>
            <th>开奖结果</th>
            <td>
                <input type="text" class="am-form-field result" placeholder="7个开奖号码，请用,隔开" />
                <button type="button" class="save">保存</button>
            </td>
        </tr>
    </table>
</div>

<script src="../public/all/js/result.js"></script>
<script>
    // 将最近一期的期号和开奖时间填入
    var game = "lhc";
    Result.switchGame(game);
    Result.cb = function() {
        var curr = Result.curr();
        if (!curr) return;
        Result.cb = null;
        Result.currGame = null;

        $(".issue").val(curr.issue);
    };

    $("button.save").click(function() {
        var issue = $(".issue").val().trim();
        if (issue == "") {
            alert2("请设置开奖的期号！", true);
            return;
        }

        var arr = $("input.result").val().trim().split(",");
        if (arr.length != 7) {
            alert2("开奖号码应该为7个！", true);
            return;
        }

        for (var i = 0; i < arr.length; i++) {
            var n = arr[i];
            if (n == '' || parseInt(n) < 1 || parseInt(n) > 49) {
                alert2("开奖结果数字应该为1-49。", true)
                return;
            }
            n = parseInt(n);
            arr[i] = n;

            // 确保数字不重复
            for (var j = 0; j < i; j++) {
                if (arr[j] == arr[i]) {
                    alert2("开奖数字不能重复！", true);
                    return;
                }
            }
        } 

        // 数字处理下（前加0）
        var arr2 = [];
        for (var i in arr) {
            arr2.push(arr[i] >= 10 ? "" + arr[i]: "0" + arr[i]);
        }

        // 保存
        startLoading();
        CMD.set_lottery_result(game, $(".issue").val().trim(), arr2, function() {
            stopLoading();
            alert2("设置成功！");
        });
    });
</script>

<script>
    $(".closeTime").datetimepicker({
        format: "yyyy-mm-dd hh:ii:ss"
    });
    $(".openTime").datetimepicker({
        format: "yyyy-mm-dd hh:ii:ss"
    });

    // 得到六合彩相关的next配置
    Classify.query("lottery.lhc", "next", true, {}, function(next) {
        if (isEmpty(next)) return;
        $(".current .qihao").val(next.issue);
        $(".current .closeTime").val(next.end);
        $(".current .openTime").val(next.open);
    });

    // 保存当前期信息
    $(".save_curr").click(function() {
        var issue = $(".qihao").val().trim(),
            closeTime = $(".current .closeTime").val().trim(),
            openTime = $(".current .openTime").val().trim();
        if (issue == '' || closeTime == '' || openTime == '') {
            alert2("数据不合法！", true);
            return false;
        }

        if (openTime <= closeTime) {
            alert2("开奖时间应该迟于封盘时间！", true);
            return false;
        }

        startLoading();
        Classify.save("lottery.lhc", "next", {
            issue: issue,
            end: closeTime,
            open: openTime
        }, function(r) {
            stopLoading();
            alert2("保存成功！");
        });
    });
</script>
